/*
 * TcpServer.h
 *
 *  Created on: May 9, 2011
 *      Author: michael
 */

#ifndef TCPSERVER_H_
#define TCPSERVER_H_


#include "TcpConnection.h"
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <vector>
#include <map>

namespace DerbyServ
{

class RawData;

class TcpServer
{
public:
	typedef std::map<const int, boost::shared_ptr<TcpConnection> > Connections;
	typedef std::vector< int > ConnectionIds;

public:
	TcpServer(boost::asio::io_service & ioService_);
	virtual ~TcpServer();

	bool Send(const RawData & raw, int connectionId);
	bool Receive(RawData & raw, int connectionId);

	//void ReceiveAll(std::map<int_t, RawData> raws);

	ConnectionIds GetConnectionIds() const;

private:
	void StartAccept();
	void HandleAccept(boost::shared_ptr<TcpConnection> newConnection_,
			const boost::system::error_code & error_);
	int GenerateConnectionId() const;

private:
	boost::asio::ip::tcp::acceptor _acceptor;
	Connections _connections;
};

} // namespace

#endif /* TCPSERVER_H_ */
